package com.elean.springboot.superprint.autoconfigure.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @description:
 * @author:tanhuiwen
 * @date:2021-06-25
 **/

public class DigestMessageUtil {

    /**
     * Sha256加码 ，默认UTF8编码
     *
     * @param inStr
     *            原文
     * @return 密文
     */
    public static String sha256Message(String inStr) {
        return sha256Message(inStr, "UTF-8");
    }

    /**
     * Sha256加码
     *
     * @param inStr
     *            原文
     * @param charsetName
     *            编码
     * @return 密文
     */
    public static String sha256Message(String inStr, String charsetName) {
        charsetName = Charset.isSupported(charsetName) ? charsetName : "UTF-8";

        byte[] md5Bytes = null;
        try {
            md5Bytes = sha256Message(inStr.getBytes(charsetName));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return byteArr2HexStr(md5Bytes);
    }

    /**
     * Sha256加码
     *
     * @param originalData
     *            源字节
     * @return Sha256后字节
     */
    public static byte[] sha256Message(byte[] originalData) {
        return digestMessage(originalData, "SHA-256");
    }

    /**
     * MD5加码 生成32位md5码，默认UTF8编码
     *
     * @param inStr
     *            原文
     * @return 密文
     */
    public static String md5Message(String inStr) {
        return md5Message(inStr, "UTF-8");
    }

    /**
     * MD5加码 生成32位md5码
     *
     * @param inStr
     *            原文
     * @param charsetName
     *            编码
     * @return 密文
     */
    public static String md5Message(String inStr, String charsetName) {
        charsetName = Charset.isSupported(charsetName) ? charsetName : "UTF-8";

        byte[] md5Bytes = null;
        try {
            md5Bytes = md5Message(inStr.getBytes(charsetName));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return byteArr2HexStr(md5Bytes);
    }

    /**
     * MD5加码
     *
     * @param originalData
     *            源字节
     * @return MD5后字节
     */
    public static byte[] md5Message(byte[] originalData) {
        return digestMessage(originalData, "MD5");
    }

    private static byte[] digestMessage(byte[] originalData, String algorithm) {
        byte[] bytes = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            messageDigest.update(originalData);
            bytes = messageDigest.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public static String byteArr2HexStr(byte[] arrB) {
        int iLen = arrB.length;
        // 每个byte用两个字符才能表示，所以字符串的长度是数组长度的两倍
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
            // 把负数转换为正数
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            // 小于0F的数需要在前面补0
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString().toUpperCase();
    }

}